﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using System.Xml.Linq;
using System.Configuration;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
using System.Globalization;

namespace jobquery
{
    class jobquery
    {
        //name of job table
        private const string TABLE = "jobtable";
        
        // set these constants with your values to run the sample.
        
        private const string _storageacc = "<storage account name>";
        private const string _storagekey = "<storage account key>";

        //Connection Strings

        private const string _connectionstring = "<storage connection string>";

        
        public class JobEntity : TableEntity
        {
            public JobEntity(string node, string jobid)
            {
                this.PartitionKey = node;
                this.RowKey = jobid;
            }
            public JobEntity() { }
            public string Status { get; set; }
            public DateTime TimeIn { get; set; }
            public DateTime TimeUpdated { get; set; }
        }


        static int Main(string[] args)
        {
            string table = "";
            string jobid = "";
            
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: jobquery <job table>  <job id | ALL>");
                return 1;
            }
            else
            {
                table = args[0];
                jobid = args[1];
            }
            
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionstring);
           
            CloudTableClient tableclient = storageAccount.CreateCloudTableClient();
            CloudTable jobtable = tableclient.GetTableReference(TABLE);
            if (jobid == "ALL")
            {
                TableQuery<JobEntity> newquery = new TableQuery<JobEntity>();
                var results = jobtable.ExecuteQuery(newquery);
                Console.WriteLine("Node\t\tJobID\t\t\tStatus\t\tTimeIn\t\t\tTimeUpdated");
                foreach (var item in results)
                {
                    Console.WriteLine("{0:-10}\t\t{1:-16}\t{2:-10}\t{3:G}\t{4:G}", item.PartitionKey, item.RowKey, item.Status, item.TimeIn, item.TimeUpdated);
                } 
            }
            else
            {
                TableQuery<JobEntity> newquery = new TableQuery<JobEntity>().Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, jobid));
                var results = jobtable.ExecuteQuery(newquery);
                foreach (var item in results)
                {
                    Console.WriteLine("{0:-10}\t{1:-16}\t{2:-10}\t{3:G}\t{4:G}", item.PartitionKey, item.RowKey, item.Status, item.TimeIn, item.TimeUpdated);
                } 
            }
            return 0;

        }
    }
}
